Project: Scrabble Bot

You better watch out Nana, I can beat you at scrabble now.

Connor Lantz

How it Began

In the summer of 2022, a significant Scrabble loss to my Nana ignited a quest for redemption. Faced with two options – master the game or engineer a Scrabble bot to excel on my behalf – I chose the latter.

This project's objective was to craft a Scrabble bot capable of making highly effective word choices. To streamline the challenge, I excluded the 'any letter' tile, focusing on word selection rather than playing entire games. User familiarity with Scrabble rules was expected, with minimal emphasis on user-friendliness in the interface.

The project unfolded as a complex endeavor, quite distinct from my previous Wordle bot. Scrabble's multidimensional nature, with countless possibilities, required a systematic approach.

How I did it

  • Create the virtural playing board

    Begin by setting up a virtual 15x15 game board, which serves as the foundation for playing the game. This board is essential for visualizing and managing the game state.

  • Word Placement and Validation

    Allow the user to place a word on the board by interacting with a series of prompts. These prompts determine the starting coordinate, direction, and the word to be played. Validate the user's input to ensure it meets the following criteria:
    1. The play stays within the 15x15 board boundaries.
    2. Existing words on the board are not overwritten.
    3. Check the validity of each row and column individually, ensuring that all words formed are present in the dictionary. This step accounts for multiple words created in one play.
  • Scoring Plays

    Implement a scoring system for each play based on Scrabble's specific scoring guidelines. This is achieved through the following steps:
    1. Identify new words on the board resulting from the user's play by comparing the board before and after the play.
    2. Extract coordinates of the new words and consider any word or letter multipliers present.
    3. Score each word letter by letter, applying multipliers as necessary.
    4. Sum up the scores of all new words created.
    5. If the user uses all 7 tiles in their play, award an additional 50 points for achieving a "bingo."
  • Making a scrabble bot

    Design a bot which can take a variable number of letters, provided by the user, and suggest several valid moves which will yield the maximum number of points.
    1. Begin by breaking down each row and column into a string of letters and blank spaces. For instance, consider a row on the board as follows: "A _ _ _ S T R I N G _ _ _ I S."
    2. Identify potential starting points for new words within the row or column. In the example above, you can theoretically begin a word at positions like: "A _ _ _ S T R I N G _ _ _ I S." A word can only start where no letter is present in the preceding tile.
    3. For each starting position, establish a set of constraints that can be applied to words in the Scrabble dictionary. For instance, if focusing on the 'S' in the given example, constraints might be: "the first six letters are STRING, the next 3 letters can be any, and the last two letters are IS." The word does not need to occupy the entire section, but if it does, these constraints must be met. Example guesses for this spot could be STRINGS or STRINGY.
    4. For each possible move, eliminate those that contain letters not present in the user's current tile rack.
    5. Utilize the previously created validation function to validate all identified possible moves. This step is crucial as the potential words are initially chosen considering only one dimension. Adjacent rows or columns may affect the validity of the guess.
    6. Once a move is validated, apply it to the point calculator developed earlier to calculate the score.
    7. Present the user with the top 10 plays to enable them to select the most suitable move.

How it ends?

This project has been a significant challenge, as it was developed using basic Python functions without relying on advanced techniques. If you're interested in inspecting the code, I've attached it here, along with the dictionary here. The breakdown above provides a high-level overview, but there are numerous technical intricacies involved in achieving these processes. I take pride in creating an intuitive solution for this complex problem. On average, my computer can generate a guess in about 90 seconds, depending on the board's complexity, and will typically average around 500 points in 2-player Scrabble, if decent letters are picked.

In the future, I plan to incorporate a feature allowing the use of 'any letter' tiles. However, I'm cautious about potential performance issues that may arise, as it could extend the time needed to make a guess, potentially impacting the user's experience. Additionally, I aim to enhance the user interface to make the program more user-friendly and intuitive.

If you intend to download the Python file and its associated dictionary, please ensure they are placed in the same folder for the bot to access. To use the program, press 'play,' and you can make a move using 'z.move()' or engage the bot with 'z.bestMove()' in the command line. Enjoy the game!